package nju.mscs.businesslogic;

import nju.mscs.data.DataServerManager;
import nju.mscs.data.object.Database;
import nju.mscs.data.object.Date;
import nju.mscs.data.object.User;
import nju.mscs.data.object.VipConsumer;

public class AM implements AccountManager{
	private String id;
	private Database db;
	public AM(){
		db=LogIn.db;
	}
	public String[] createNewUser(String name,String password,String position,String adress){
		int pos=-1;
		if(position.equals("收银员"))
			pos=User.CASHIER;
		else if(position.equals("分店经理"))
			pos=User.SHOP_MANAGER;
		else if(position.equals("总经理"))
			pos=User.GENERAL_MANAGER;
		else if(position.equals("系统管理员"))
			pos=User.SYSTEM_MANAGER;
		String id=createUserID(pos);
		db.addUser(name, password, id,pos,Integer.parseInt(adress));
		String[] result={id,name,position,password};
		return result;
	}
	public boolean deleteUser(String id){
		db.deleteUser(id);
		return true;
	}
	public String addVipConsumer(String name,String[] birth,String sex,String phone,String sumpoint,String notation){
		String id=createVipConsumerID();
		Date bir=new Date(Integer.parseInt(birth[0]),Integer.parseInt(birth[1]),Integer.parseInt(birth[2]));
		boolean se=false;
		if(sex.equals("男"))
			se=VipConsumer.MALE;
		else if(sex.equals("女"))
			se=VipConsumer.FEMALE;
		db.addVipConsumer(name, id, se, Integer.parseInt(phone), bir);
		db.requestAddVip(name, id, se, Integer.parseInt(phone), bir);
		return id;
	}
	public boolean deleteVipConsumer(String id){
		db.deleteVipConsumer(id);
		db.requsetDeleteVip(id);
		return true;
	}
	public boolean changeVipConsumer(String[] infor){
		VipConsumer vip=db.searchVipConsumerById(infor[0]);
		vip.setName(infor[1]);
		String[] birth=infor[2].split("/");
		Date da=new Date(Integer.parseInt(birth[0]),Integer.parseInt(birth[1]),Integer.parseInt(birth[2]));
		vip.setBirth(da);
		boolean se=false;
		if(infor[3].equals("男"))
			se=VipConsumer.MALE;
		else if(infor[3].equals("女"))
			se=VipConsumer.FEMALE;
		vip.setSex(se);
		vip.setTelephone(Integer.parseInt(infor[4]));
		vip.setSumPoints(Integer.parseInt(infor[5]));//有待添加restPoint
		db.requestChangeVip(infor[1], infor[0], se, Integer.parseInt(infor[4]), da);
		//加备注
		return true;
	}
	private String createVipConsumerID(){
		String id=""+(Integer.parseInt(LogIn.num)*1000+db.getAmountOfVip());//+db.getUserList();//生成id,四位
		return id;
	}
	private String createUserID(int position){
		String id=""+(db.getAmountOfUser()*10+position+1000);//生成id
		return id;
	}

	public boolean changeUser(String[] infor) {
		User user=db.searchUserById(infor[0]);
		user.setName(infor[1]);
		user.setPassword(infor[2]);
		System.out.println("AS:"+infor[2]);
		int type=-1;
		if(infor[3].equals("收银员"))
			type=User.CASHIER;
		else if(infor[3].equals("分店经理"))
			type=User.SHOP_MANAGER;
		else if(infor[3].equals("总经理"))
			type=User.GENERAL_MANAGER;
		else if(infor[3].equals("系统管理员"))
			type=User.SYSTEM_MANAGER;
		user.setType(type);
		// TODO Auto-generated method stub
		return true;
	}
}
